//
//  MasterInfoView.swift
//  HinsLive
//
//  Created by haoxian on 2019/4/28.
//  Copyright © 2019 Hins. All rights reserved.
//

import UIKit


final class MasterInfoView: UIView {
    private(set) lazy var avatarImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.layer.cornerRadius = 40.0 / 2
        imageView.layer.masksToBounds = true
        imageView.contentMode = .scaleAspectFill
        imageView.backgroundColor = .imagePlaceholderColor
        return imageView
    }()
    
    private(set) lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont(name: "Helvetica-Bold", size: 15)
        label.textColor = .white
        label.textAlignment = .left
        return label
    }()
    
    private lazy var coverLayer: CAShapeLayer = {
        let coverLayer = CAShapeLayer()
        coverLayer.fillColor = UIColor.roomGrayBackgroundColor.cgColor
        return coverLayer
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        coverLayer.frame = bounds
        coverLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: height/2).cgPath
    }
    
    func setInfo(masterName: String, avatarUrl: String) {
        nameLabel.text = masterName
        avatarImageView.kf.setImage(with: URL(string: avatarUrl))
    }
}

private extension MasterInfoView {
    func setupUI() {
        layer.addSublayer(coverLayer)
        addSubview(avatarImageView)
        avatarImageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(5)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(40)
        }
        addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.height.equalTo(avatarImageView.snp.height)
            make.centerY.equalToSuperview()
            make.left.equalTo(avatarImageView.snp.right).offset(5)
            make.right.equalToSuperview().offset(-25)
        }
    }
}
